package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.entity.TokenDecode;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/car")
public class CarController {
    @Autowired
    private CarService carService;


    @GetMapping(value = "/add")
    public Result add(Integer num, Long id) {
        Map<String, String> userInfo = TokenDecode.getUserInfo();
        String username = userInfo.get("username");
        carService.add(num, id, username);
        return new Result(true, StatusCode.OK, "加入购物车成功");
    }

    @GetMapping("/list")
    public Result list() {
//        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
//        String tokenValue = details.getTokenValue();
        Map<String, String> userInfo = TokenDecode.getUserInfo();
        String username = userInfo.get("username");
        //查询购物车列表
        List<OrderItem> orderItems = carService.list(username);
        return new Result<List<OrderItem>>(true, StatusCode.OK, "购物车查询成功", orderItems);
    }
}